/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gui;
import java.util.Vector;
import javax.microedition.lcdui.*;
import org.kxml.kdom.*;
import parser.CargaXML;
/**
 * @author Renato
 */
public class MenuCartelera extends Canvas implements CommandListener {
    Vector listaCartelera = new Vector(5, 2);
    Cine cine;
    MenuCines mc;
    FuncionalidadMenuCartelera fmc;
    FormularioCompra fc;
    private int width,height;
    private int anchor = Graphics.TOP | Graphics.LEFT;
    private MenuPrincipal mp;
    private Mapa m;
    private MenuCartelera cm;
    private int visibles = 4;
    private int barTitulo = 20;
    private int altoVisible;
    private int posFlags = 15;
    final String  url = "http://www.ngwise.com/cm/cartelera.php";
    final String PARAMETRO = "idcine";
    public MenuCartelera(Cine cine, MenuCines mc) {
        try {
	    this.cine = cine;
            this.mc = mc;
            mp = mc.getMp();
            width = getWidth();
            height = getHeight();
	    setCommandListener(this);
	    addCommand(new Command("Regresar", Command.BACK, 1));
            altoVisible =  height - barTitulo ;
            obtenCartelera(cine);

        } catch(Exception e) {
            e.printStackTrace();
        }
    } 
    public void obtenCartelera(Cine cine){
        listaCartelera.removeAllElements();
        Element e = CargaXML.obtenRoot(url,this.PARAMETRO,cine.getId() );
        for(int i=0;i<e.getChildCount();i++){
            Element peli = e.getElement(i);
            String titulo = peli.getElement("titulo").getText();
            String hora = peli.getElement("hora").getText();
            int costopb = Integer.parseInt(  peli.getElement("costopb").getText() );
            listaCartelera.addElement(new Pelicula(titulo, hora, costopb));
        }
        fmc = new FuncionalidadMenuCartelera( 0 , listaCartelera.size() - 1, 0, visibles);
        
    }
    public void paint(Graphics g) {
        
        limpiaPantalla(g);
        Font f = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);
        g.setFont(f);
        g.setColor(0x32639A);
        /*Titulo*/
        String titulo = "Cartelera";
        int anchoTitulo = f.stringWidth(titulo);
        int altoTitulo = f.getHeight();
        int posXt = (width - anchoTitulo)/2;
        int posYt = barTitulo- altoTitulo;
        g.drawString(titulo,posXt ,posYt , anchor);
        g.setColor(0x4486C7);
        g.setStrokeStyle(Graphics.SOLID);
        g.drawLine(0, barTitulo - 1, width, barTitulo -1);
        g.drawLine(0, barTitulo, width, barTitulo);

        Font f2 = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        g.setFont(f2);
        int nW =(int)(width);
        int nH = ((int)(altoVisible)   )/visibles - (posFlags*2)/visibles - 23/visibles - 5;
        int deltax = (int)(width*0.00);
        int deltay = barTitulo + posFlags;
        int y;
        int k = 0;
        int posXf = width / 2;
        int posYfu=  barTitulo;
        int posYfd=  barTitulo + (nH +5)*visibles + posFlags ;
        if(fmc.hayAnterior()){
            dibujaTrianguloColorUp(g, posXf,posYfu);
        }else{
            dibujaTrianguloUp(g, posXf,posYfu);
        }
        if(fmc.haySiguiente()){
            dibujaTrianguloColorDown(g, posXf,posYfd);
        }else{
            dibujaTrianguloDown(g, posXf,posYfd);
        }

        for(int i=fmc.getInicial();i<=fmc.getFinal();i++){
            y = deltay +  (nH+ 5)*k;
            int delayf = deltax;
            
            if(fmc.getActual() == i)
                dibujaSeleccionado( g, deltax, delayf, y, nW, nH,   i);
            else
                dibujaNoSeleccionado( g, deltax, delayf, y, nW, nH,  i);

            k++;

        }
    }
    public void dibujaTrianguloDown(Graphics g, int posXf, int posYfd){
        g.drawLine(posXf - 5, posYfd + 2, posXf + 5, posYfd + 2);
        g.drawLine(posXf + 5, posYfd + 2, posXf, posYfd + posFlags - 2);
        g.drawLine( posXf, posYfd + posFlags - 2, posXf - 5, posYfd + 2);

    }
    public void dibujaTrianguloUp(Graphics g,int posXf,int posYfu){
        g.drawLine(posXf, posYfu + 2, posXf + 5, posYfu + posFlags - 2);
        g.drawLine(posXf + 5, posYfu + posFlags - 2, posXf - 5, posYfu + posFlags - 2);
        g.drawLine(posXf - 5, posYfu + posFlags - 2, posXf, posYfu + 2);
    }
    public void dibujaTrianguloColorDown(Graphics g,int posXf,int posYfd){
        g.fillTriangle(posXf - 5, posYfd + 2, posXf + 5, posYfd + 2, posXf, posYfd + posFlags - 2);
    }
    public void dibujaTrianguloColorUp(Graphics g,int posXf,int posYfu){
          g.fillTriangle(posXf, posYfu + 2, posXf + 5, posYfu + posFlags - 2, posXf - 5, posYfu + posFlags - 2);
    }
    public void dibujaSeleccionado(Graphics g,int deltax,int delayf,int y,int nW,int nH,int  i){
        Pelicula p = (Pelicula)listaCartelera.elementAt(i);
        g.setColor(0xCCCCFF);
        g.fillRect( deltax, y , nW, nH );
        g.setColor(0xFFFFFF);
        Font f2 = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        g.setFont(f2);
        g.drawString(p.getTitulo(), delayf, y , anchor);
        Font f3 = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL);
        g.setFont(f3);
        g.drawString("Hora " + p.getHora(), delayf, y + nH/2, anchor);
    }
    public void dibujaNoSeleccionado(Graphics g,int deltax,int delayf,int y,int nW,int nH,int  i){
        Pelicula p = (Pelicula)listaCartelera.elementAt(i);
        g.setColor(0xE8E8E8  );
        g.fillRect( deltax, y , nW, nH );
        g.setColor(0xFFFFFF);
        Font f2 = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        g.setFont(f2);
        g.drawString(p.getTitulo(), delayf, y , anchor);
        Font f3 = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL);
        g.setFont(f3);
        g.drawString("Hora " + p.getHora(), delayf, y + nH/2, anchor);
    }
    protected  void keyPressed(int keyCode) {
         String str = getKeyName(keyCode);
        if (str.equals("Up") || str.equals("UP")) {
            fmc.subir();
            repaint();
        }if (str.equals("Down") || str.equals("DOWN")) {
            fmc.bajar();
            repaint();
        }if(getGameAction(keyCode)==FIRE ||str.toUpperCase().equals("LEFT")){
            int i = fmc.getActual();
             Pelicula p = (Pelicula)listaCartelera.elementAt(i);
             fc = new FormularioCompra("Compra De Boletos En Linea", p, this);
             if(fc == null)
                   System.out.println("nulo");
             mp.getMidlet().pantalla.setCurrent(fc);
        }
    }
   
    public void commandAction(Command command, Displayable displayable) {
         if(command.getLabel().equals("Regresar")){
            mc.getMp().getMidlet().pantalla.setCurrent(mc);
        }
    }
    public void limpiaPantalla(Graphics g){
        g.setColor( 255, 255, 255 );
        g.fillRect( 0, 0, width, height );
    }
    public Cine getCine() {
        return cine;
    }

    public void setCine(Cine cine) {
        this.cine = cine;
        obtenCartelera(cine);
    }

    public MenuCines getMc() {
        return mc;
    }

    public void setMc(MenuCines mc) {
        this.mc = mc;
    }

}